//
//  LGRehabilitationProgramController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/7/31.
//  我的--康复方案

#import "LGRecoveryProgramController.h"
//运动
#import "LGPatientSportsController.h"
//营养
#import "LGPatientNutritionController.h"
//药物
#import "LGPatientMedicineController.h"
//生活
#import "LGPatientLifeController.h"
//心理
#import "LGPatientPsychicController.h"
#define baseTop   viewPix(54)
@interface LGRecoveryProgramController ()<UIScrollViewDelegate>
/**   */
@property (nonatomic , strong)CAGradientLayer *shadowLayer;
/**   */
@property (nonatomic , strong)UIView *itemView;
/**   */
@property (nonatomic , strong)UIScrollView *baseView;
/**  运动 */
@property (nonatomic , strong)LGPatientSportsController *sportsVC;
/**  营养 */
@property (nonatomic , strong)LGPatientNutritionController *nutritionVC;
/**  药物 */
@property (nonatomic , strong)LGPatientMedicineController *medicineVC;
/**  生活 */
@property (nonatomic , strong)LGPatientLifeController *lifeVC;
/**  心理 */
@property (nonatomic , strong)LGPatientPsychicController *psychicVC;
/**   */
@property (nonatomic , strong)NSArray *itemArray;
/**   */
@property (nonatomic , assign)NSInteger currentIndex;

@end

@implementation LGRecoveryProgramController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.itemArray = @[@"运动",@"营养",@"药物",@"生活",@"心理"];
}

//模块被选中
-(void)viewWillAppearAction{
    if(self.isfirst == YES){
        [self.view.layer addSublayer:self.shadowLayer];
        [self.view addSubview:self.itemView];
        [self.view addSubview:self.baseView];
        [self createItemButtons];
        [self createItemController];
        self.currentIndex = 0;
        self.isfirst = NO;
    }
}


-(void)setCurrentIndex:(NSInteger)currentIndex{
    _currentIndex = currentIndex;
    [self.baseView setContentOffset:CGPointMake(Screen_W*currentIndex, 0)];
    for (NSInteger i=0; i<self.itemArray.count; i++) {
        NSString *borderColor = (currentIndex == i)?@"#CCCCCC":@"#F7F7F7";
        UIButton *button = [self.view viewWithTag:1000+i];
        button.selected = (currentIndex == i)?YES:NO;
        button.titleLabel.font = (currentIndex == i)?LGFontWeight(14, UIFontWeightMedium):LGFont(14);
        button.layer.borderColor = [UIColor colorWithString:borderColor].CGColor;
    }
    
    LGBaseViewController *controller = self.childViewControllers[currentIndex];
    [controller viewWillAppearAction];
}

-(void)touchButtonAction:(UIButton *)sender{
    self.currentIndex = sender.tag-1000;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.currentIndex = scrollView.contentOffset.x/Screen_W;
}

#pragma mark
#pragma mark ====> 控件布局
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    if (self.isfirst == NO) {
        CGFloat viewHeight = self.view.bounds.size.height-baseTop;
        self.baseView.frame = CGRectMake(0, baseTop, Screen_W, viewHeight);
        self.baseView.contentSize = CGSizeMake(Screen_W*self.itemArray.count, viewHeight);
        self.sportsVC.view.frame = CGRectMake(0, 0, Screen_W, viewHeight);
        self.nutritionVC.view.frame = CGRectMake(Screen_W, 0, Screen_W, viewHeight);
        self.medicineVC.view.frame = CGRectMake(Screen_W*2, 0, Screen_W, viewHeight);
        self.lifeVC.view.frame = CGRectMake(Screen_W*3, 0, Screen_W, viewHeight);
        self.psychicVC.view.frame = CGRectMake(Screen_W*4, 0, Screen_W, viewHeight);
    }
}

-(void)createItemButtons{
    CGFloat width = (Screen_W-viewPix(32)-2)/self.itemArray.count;
    for (NSInteger i=0; i<self.itemArray.count; i++) {
        UIButton *button = [UIButton buttonWithTitle:self.itemArray[i] titleFont:LGFont(14) normalColor:@"#666666" selectColor:@"#333333"];
        [button setBackgroundColor:[UIColor colorWithString:@"#F7F7F7"] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor whiteColor] forState:UIControlStateSelected];
        button.frame = CGRectMake(1+width*i, 1, width, viewPix(32)-2);
        button.layer.borderColor = [UIColor colorWithString:@"#F7F7F7"].CGColor;
        button.layer.borderWidth = 1;
        button.cornerRidus = 5;
        button.tag = 1000+i;
        [button addTarget:self action:@selector(touchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.itemView addSubview:button];
    }
}

-(void)createItemController{
    [self addChildViewController:self.sportsVC];
    [self addChildViewController:self.nutritionVC];
    [self addChildViewController:self.medicineVC];
    [self addChildViewController:self.lifeVC];
    [self addChildViewController:self.psychicVC];
    [self.baseView addSubview:self.sportsVC.view];
    [self.baseView addSubview:self.nutritionVC.view];
    [self.baseView addSubview:self.medicineVC.view];
    [self.baseView addSubview:self.lifeVC.view];
    [self.baseView addSubview:self.psychicVC.view];
}

-(CAGradientLayer *)shadowLayer{
    if(!_shadowLayer){
        _shadowLayer = [[CAGradientLayer alloc]init];
        _shadowLayer.frame = CGRectMake(0,0,Screen_W,viewPix(80));
        _shadowLayer.startPoint = CGPointMake(0.5, 0);
        _shadowLayer.endPoint = CGPointMake(0.5, 0.76);
        _shadowLayer.colors = @[(__bridge id)[UIColor colorWithString:@"#FFFFFF" alpha:1].CGColor, (__bridge id)[UIColor colorWithString:@"#FFFFFF" alpha:0].CGColor];
        _shadowLayer.locations = @[@(0), @(1.0f)];
        _shadowLayer.cornerRadius = 15;
    }
    return _shadowLayer;
}

-(UIView *)itemView{
    if(!_itemView){
        _itemView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(32))];
        _itemView.backgroundColor = [UIColor colorWithString:@"#F7F7F7"];
        _itemView.cornerRidus = 5;
    }
    return _itemView;
}

-(UIScrollView *)baseView{
    if(!_baseView){
        _baseView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, baseTop, Screen_W, Screen_H-baseTop)];
        _baseView.contentSize = CGSizeMake(Screen_W*self.itemArray.count, Screen_H-baseTop);
        _baseView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
        _baseView.showsHorizontalScrollIndicator = NO;
//        _baseView.scrollEnabled = NO;
        _baseView.pagingEnabled = YES;
        _baseView.delegate = self;
    }
    return _baseView;
}

-(LGPatientSportsController *)sportsVC{
    if(!_sportsVC){
        _sportsVC = [[LGPatientSportsController alloc]init];
        _sportsVC.patientid = self.patientid;
        _sportsVC.diseaseCode = self.diseaseCode;
    }
    return _sportsVC;
}

-(LGPatientNutritionController *)nutritionVC{
    if(!_nutritionVC){
        _nutritionVC = [[LGPatientNutritionController alloc] init];
        _nutritionVC.patientid = self.patientid;
    }
    return _nutritionVC;
}

-(LGPatientMedicineController *)medicineVC{
    if(!_medicineVC){
        _medicineVC = [[LGPatientMedicineController alloc] init];
        _medicineVC.patientid = self.patientid;
    }
    return _medicineVC;
}

-(LGPatientLifeController *)lifeVC{
    if(!_lifeVC){
        _lifeVC = [[LGPatientLifeController alloc] init];
        _lifeVC.patientid = self.patientid;
    }
    return _lifeVC;
}

-(LGPatientPsychicController *)psychicVC{
    if(!_psychicVC){
        _psychicVC = [[LGPatientPsychicController alloc] init];
        _psychicVC.patientid = self.patientid;
    }
    return _psychicVC;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
